package org.soderiakelvy.model.dao.imp;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.soderiakelvy.model.dao.SucursalPackDao;
import org.soderiakelvy.model.domain.SucursalPackVO;
import org.soderiakelvy.model.util.HibernateUtil;

public class SucursalPackDaoImp implements SucursalPackDao, Serializable{
	private static final long serialVersionUID = 9026500774707579811L;

	@Override
	public void insertSucursalPackVO(SucursalPackVO sucursalPack) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(sucursalPack);
        session.getTransaction().commit();
        session.close();
		
	}

	@Override
	public void updateSucursalPackVO(SucursalPackVO sucursalPack) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.update(sucursalPack);
        session.getTransaction().commit();
        session.close();

	}

	@Override
	public void deleteSucursalPackVO(SucursalPackVO sucursalPack) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.delete(sucursalPack);
        session.getTransaction().commit();
        session.close();
		
	}

	@Override
	public SucursalPackVO getSucursalPackVOById(long id) throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(SucursalPackVO.class);
        criteria.add(Restrictions.eq("sucursalPackId", id)).uniqueResult();
        SucursalPackVO spVO = (SucursalPackVO) criteria.uniqueResult();
        session.close();
        return spVO;
	}

	@Override
	public List<SucursalPackVO> getAllSucursalPackVO() throws Exception {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(SucursalPackVO.class);
        criteria.addOrder(Order.asc("sucursalPackId"));
        List<SucursalPackVO> sucursalPack = criteria.list();
        session.close();
	    return sucursalPack;
	}
}
